package com.castle.fortress.admin.dify.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.dify.model.ContractReviewInfo;
import com.castle.fortress.admin.dify.service.ContractReviewInfoService;
import com.castle.fortress.common.entity.RespBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/contract-review-info")
public class ContractReviewInfoController {

    @Autowired
    private ContractReviewInfoService contractReviewInfoService;

    @GetMapping("/{id}")
    public ContractReviewInfo getById(@PathVariable Long id) {
        return contractReviewInfoService.getById(id);
    }

    @GetMapping("/contract/{contractId}")
    public List<ContractReviewInfo> getByContractId(@PathVariable Long contractId) {
        return contractReviewInfoService.getByContractId(contractId);
    }

    @GetMapping("/page")
    public Page<ContractReviewInfo> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long contractId,
            @RequestParam(required = false) String reviewStatus) {
        
        LambdaQueryWrapper<ContractReviewInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (contractId != null) {
            queryWrapper.eq(ContractReviewInfo::getContractId, contractId);
        }
        if (reviewStatus != null) {
            queryWrapper.eq(ContractReviewInfo::getReviewStatus, reviewStatus);
        }
        queryWrapper.orderByDesc(ContractReviewInfo::getReviewStartTime);
        
        return contractReviewInfoService.page(new Page<>(current, size), queryWrapper);
    }

    @PostMapping("/start")
    public boolean startReview(@RequestParam Long contractId, @RequestParam Long ruleId) {
        return contractReviewInfoService.startReview(contractId, ruleId);
    }

    @PostMapping("/complete/{id}")
    public boolean completeReview(@PathVariable Long id) {
        return contractReviewInfoService.completeReview(id);
    }

    @PostMapping
    public boolean save(@RequestBody ContractReviewInfo info) {
        return contractReviewInfoService.save(info);
    }







    @PostMapping("/saveBatch")
    public RespBody<String> saveBatch(@RequestBody List<ContractReviewInfo> infos) {



      return RespBody.data(contractReviewInfoService.saveBatch(infos)+"")  ;


    }





    @PutMapping("/{id}")
    public boolean update(@PathVariable Long id, @RequestBody ContractReviewInfo info) {
       // info.setId(id);
        return contractReviewInfoService.updateById(info);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return contractReviewInfoService.removeById(id);
    }
} 